package top.liuyao.lab.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.liuyao.lab.mapper.UserMapper;
import top.liuyao.lab.pojo.User;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    UserMapper userMapper;
    Boolean flag = false;

    @PostMapping("/land")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session) {
        List<User> users = userMapper.users();
        Iterator<User> iterator = users.iterator();
        while (iterator.hasNext()) {
            User user = iterator.next();
            if (user.getName().equals(username) && user.getPwd().equals(password)) {
                session.setAttribute("LoginUser", username);
                return "redirect:/success.html";
            } else if (!iterator.hasNext()) {
                model.addAttribute("msg", "账号或者密码错误");
                return "login";
            }
        }
        return "login";
    }
    @ResponseBody
    @PostMapping("/toLogin")
    public String toLogin(@RequestParam(value = "name",required=false)String name,@RequestParam(value = "pwd",required=false) String pwd){
        List<User> users = userMapper.users();
        Iterator<User> iterator = users.iterator();
        String msg = "";
        if (name!=null && pwd!=null){
            while (iterator.hasNext()) {
                User user = iterator.next();
                if (user.getName().equals(name) && user.getPwd().equals(pwd)){
                    msg = "success";
                    break;
                }else {
                    msg = "账号密码错误";
                }
            }
        }
        return msg;
    }
    @PostMapping("/register")
    public String register(@RequestParam("name") String name, @RequestParam("pwd") String pwd) {
        if (flag){
            HashMap<String, Object> map = new HashMap<>();
            map.put("name", name);
            map.put("pwd", pwd);
            userMapper.addUser(map);
            System.out.println("添加成功");
        }
        return "login";
    }


    @ResponseBody
    @PostMapping("/toReg")
    public String toReg(@RequestParam(value = "name",required=false)String name){
        List<User> users = userMapper.users();
        Iterator<User> iterator = users.iterator();
        String msg = "";
        if (name!=null) {
            while (iterator.hasNext()) {
                User user = iterator.next();
                if (user.getName().equals(name)) {
                    msg = "无效账户已存在";
                    flag = false;
                    break;
                } else {
                    msg = "该账号有效";
                    flag = true;
                }
            }
        }
        return msg;
    }

}
